package com.luoying.itew.fragment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.luoying.itew.R;
import com.luoying.itew.activity.SignInActivity;
import com.luoying.itew.adapter.InspectLogAdapter;
import com.luoying.itew.bean.InspectLogBean;
import com.luoying.itew.common.AntaiApplication;
import com.luoying.itew.event.SubmitLogSuccessEvent;
import com.luoying.itew.http.HttpRequestUtils;
import com.luoying.itew.http.HttpResponseCallBack;
import com.luoying.itew.manage.UserManage;
import com.luoying.itew.util.LogUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 巡查日志
 * Created by Administrator on 2018/5/24.
 * time 2018-5-10
 * author zm
 */
public class InspectLogFragment extends BaseFragment implements OnRefreshLoadMoreListener {

    public static final String TAG = "InspectLogFragment";

    @BindView(R.id.inspect_log_refresh)
    SmartRefreshLayout inspectLogRefresh;
    @BindView(R.id.MyListView_news)
    RecyclerView recyclerView;
    @BindView(R.id.iv_log_add)
    ImageView ivLogAdd;

    private int count = 1;
    private InspectLogAdapter adapter;
    private List<InspectLogBean.PaginationBean.RecordsBean> records;

    @Override
    protected int getLayoutResId() {
        return R.layout.inspect_log_fragment;
    }

    @Override
    protected void init() {
        super.init();
        EventBus.getDefault().register(this);
        int userType = UserManage.getInstance().getUserInfo().getUserType();
        int userLevel = UserManage.getInstance().getUserInfo().getUserLevel();
        if (userLevel == 7 || userType == 3){ //为巡查人员，就显示监测按钮
            ivLogAdd.setVisibility(View.VISIBLE);
        }
        sendRequest();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(SubmitLogSuccessEvent event) {
        sendRequest();
    }

    private void sendRequest() {
        HttpRequestUtils.getInstance().getLog(AntaiApplication.cuurMonitorId+"",count,
                new HttpResponseCallBack<Map>() {
                    @Override
                    public void onFail(String message) {
                        LogUtils.d(TAG,"onFail == " + message);
                    }

                    @Override
                    public void onSuccess(Map bean) {
                        String response = new Gson().toJson(bean);
                        LogUtils.d(TAG,response);
                        try {
                            InspectLogBean inspectLogBean = new Gson().fromJson(response, InspectLogBean.class);
                            setData(inspectLogBean);
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String message) {
                        LogUtils.d(TAG,"onError == " + message);
                    }
                });
    }

    private void setData(InspectLogBean inspectLogBean) {
        records = inspectLogBean.getPagination().getRecords();
        setRecycleView();
    }

    private void setRecycleView() {
        if (records!=null&&records.size() !=0){
            if (count != 1) {
                adapter.addList(records);
                return;
            }
            adapter = new InspectLogAdapter(getContext(), records);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            if (recyclerView == null) {
                return;
            }
            if (linearLayoutManager != null) {
                recyclerView.setLayoutManager(linearLayoutManager);
            }
            if (adapter != null) {
                recyclerView.setAdapter(adapter);
            }
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        ivLogAdd.setEnabled(true);
    }

    @OnClick(R.id.iv_log_add)
    public void onViewClicked() {
        ivLogAdd.setEnabled(false);
        ClimbTo(SignInActivity.class);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        count++;        //每次触发上拉操作就页数+1
        refreshLayout.finishLoadMore(1000);
        sendRequest();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        //上拉刷新，意味着刷新所有数据，pageCount置为1
        count = 1;
        refreshLayout.finishRefresh(2000);
        sendRequest();
    }
}
